<?php
declare (strict_types = 1);

namespace app\controller\api\v1;

use think\Request;
use think\Validate;
use app\validate\TestValidate;
use think\exception\ValidateException;

class TestValidateController
{

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        // demo 演示的是自定义表单验证

        $username = $request->post('username');
        $user_id  = $request->post('user_id');
        $email    = $request->post('email');
        $amount   = $request->post('amount');

        // 验证参数
        $check_data = [
            'username'  => $username,
            'user_id'   => $user_id,
            'email'     => $email,
            'amount'    => $amount,
        ];
        // 设置验证规则
        $rules = [
            'username|用户名' => 'require|alpha',
            'user_id|用户ID'  => 'require|integer|gt:0',
            'email|邮箱'      => 'require|email',
            'amount|数量'     => 'require|float|gt:0'
        ];
        // 设置验证提示信息
        $message = [
            'username.require'  => ':attribute 不能为空',
            'username.alpha'    => ':attribute 必须为字母',
            'user_id.require'   => ':attribute 不能为空',
            'user_id.integer'   => ':attribute 必须为整数',
            'user_id.gt'        => ':attribute 必须大于零',
            'email.require'     => ':attribute 不能为空',
            'email.email'       => ':attribute 格式错误',
            'amount.require'    => ':attribute 不能为空',
            'amount.float'      => ':attribute 必须为浮点数',
            'amount.gt'         => ':attribute 必须大于零',
        ];

        $validate = new Validate;
        $validate->message($message);
        // 验证数据,验证通过返回 true
        if($validate->check($check_data, $rules) === false)
        {
            $error = [
                'message'   => $validate->getError(),
                'data'      => null,
                'code'      => 200,
            ];
            return json($error, 403);
        }

        $data = [
            'message'   => str_replace('\\', '/', __CLASS__.'->'.__FUNCTION__.'()'),
            'data'      => null,
            'code'      => 200,
        ];
        return json($data, 200);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $username   = $request->post('username');
        $user_id    = $request->post('user_id/d');
        $email      = $request->post('email');
        $amount     = $request->post('amount');

        $check_data = [
            'username'  => $username,
            'user_id'   => $user_id,
            'email'     => $email,
            'amount'    => $amount
        ];

        try {
            validate(TestValidate::class)->check($check_data);
        } catch (ValidateException $validateException){
            $response = [
                'code'      => 200,
                'message'   => $validateException->getError(),
                'data'      => null
            ];
            return json($response, 403);
        }
    }
}
